package com.qk.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/courses")
public class CourseController {
    @Autowired
    private CourseService courseService;
    //新增课程
    @PostMapping
    public Result add(@RequestBody Course course){//@RequestBody作用就是把json数据转换成对象
//     System.out.println("新增课程"+course);
        log.info("新增课程:{}",course);
        courseService.add(course);
        return Result.success();
    }

//课程列表查询
@GetMapping
public Result list( Integer subject,String  name, Integer target,
                   @RequestParam Integer page,
                   @RequestParam Integer pageSize){
    log.info("查询列表:{},{},{},{},{}",subject,name,target,page,pageSize);
//调用Service分页查询
    PageResult<Course> pageResult = courseService.list(subject,name,target,page,pageSize);

    return Result.success(pageResult);
}
    //根据ID查询
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
//     System.out.println("查询id:"+id);
        log.info("查询id:{}",id);
        Course course = courseService.getById(id);
        return Result.success(course);
    }

    // 修改课程信息
    @PutMapping
    public Result update(@RequestBody Course course){
//     System.out.println("修改课程信息:"+course);
        log.info("修改部门信息:{}",course);
        courseService.update(course);
        return Result.success();
    }

    // 删除课程
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
//     System.out.println("删除课程:"+id);
        log.info("删除课程:{}",id);
        courseService.deleteById(id);
        return Result.success();
    }

//查询所有课程
    @GetMapping("/list")
    public Result select(){
        log.info("查询所有课程");
        List<Course> courseList = courseService.selectAll();
        return Result.success(courseList);
    }

//    根据学科查询课程
    @GetMapping("/subject/{subject}")
    public Result selectBySubject(@PathVariable Integer subject){
        log.info("查询课程:{}",subject);
        List<Course> courseList = courseService.selectBySubject(subject);
        return Result.success(courseList);
    }
}
